{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用tushare包获取某股票的历史行情数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = ts.get_k_data(\"600519\",start=\"1988-01-01\")\n",
    "df.to_csv(\"600519.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用date作为索引，只要需要的open（开盘价）、close（收盘价）、high（最高价）、low（最低价）\n",
    "df = pd.read_csv(\"600519.csv\", index_col=\"date\",parse_dates=['date'])[['open','close','high','low']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2001-08-27</th>\n",
       "      <td>5.392</td>\n",
       "      <td>5.554</td>\n",
       "      <td>5.902</td>\n",
       "      <td>5.132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-28</th>\n",
       "      <td>5.467</td>\n",
       "      <td>5.759</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-29</th>\n",
       "      <td>5.777</td>\n",
       "      <td>5.684</td>\n",
       "      <td>5.781</td>\n",
       "      <td>5.640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-30</th>\n",
       "      <td>5.668</td>\n",
       "      <td>5.796</td>\n",
       "      <td>5.860</td>\n",
       "      <td>5.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-08-31</th>\n",
       "      <td>5.804</td>\n",
       "      <td>5.782</td>\n",
       "      <td>5.877</td>\n",
       "      <td>5.749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-03</th>\n",
       "      <td>5.812</td>\n",
       "      <td>5.779</td>\n",
       "      <td>5.870</td>\n",
       "      <td>5.757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-04</th>\n",
       "      <td>5.782</td>\n",
       "      <td>5.852</td>\n",
       "      <td>5.949</td>\n",
       "      <td>5.762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-05</th>\n",
       "      <td>5.876</td>\n",
       "      <td>5.849</td>\n",
       "      <td>5.924</td>\n",
       "      <td>5.813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-06</th>\n",
       "      <td>5.835</td>\n",
       "      <td>5.734</td>\n",
       "      <td>5.854</td>\n",
       "      <td>5.704</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-07</th>\n",
       "      <td>5.702</td>\n",
       "      <td>5.574</td>\n",
       "      <td>5.773</td>\n",
       "      <td>5.570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-10</th>\n",
       "      <td>5.531</td>\n",
       "      <td>5.734</td>\n",
       "      <td>5.757</td>\n",
       "      <td>5.470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-11</th>\n",
       "      <td>5.749</td>\n",
       "      <td>5.670</td>\n",
       "      <td>5.773</td>\n",
       "      <td>5.656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-12</th>\n",
       "      <td>5.520</td>\n",
       "      <td>5.621</td>\n",
       "      <td>5.656</td>\n",
       "      <td>5.515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-13</th>\n",
       "      <td>5.626</td>\n",
       "      <td>5.607</td>\n",
       "      <td>5.671</td>\n",
       "      <td>5.577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-14</th>\n",
       "      <td>5.626</td>\n",
       "      <td>5.671</td>\n",
       "      <td>5.702</td>\n",
       "      <td>5.593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-17</th>\n",
       "      <td>5.637</td>\n",
       "      <td>5.599</td>\n",
       "      <td>5.670</td>\n",
       "      <td>5.546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-18</th>\n",
       "      <td>5.606</td>\n",
       "      <td>5.663</td>\n",
       "      <td>5.710</td>\n",
       "      <td>5.601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-19</th>\n",
       "      <td>5.671</td>\n",
       "      <td>5.768</td>\n",
       "      <td>5.768</td>\n",
       "      <td>5.634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-20</th>\n",
       "      <td>5.765</td>\n",
       "      <td>5.720</td>\n",
       "      <td>5.788</td>\n",
       "      <td>5.702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-21</th>\n",
       "      <td>5.668</td>\n",
       "      <td>5.634</td>\n",
       "      <td>5.718</td>\n",
       "      <td>5.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-24</th>\n",
       "      <td>5.634</td>\n",
       "      <td>5.632</td>\n",
       "      <td>5.685</td>\n",
       "      <td>5.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-25</th>\n",
       "      <td>5.668</td>\n",
       "      <td>5.660</td>\n",
       "      <td>5.709</td>\n",
       "      <td>5.632</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-26</th>\n",
       "      <td>5.642</td>\n",
       "      <td>5.637</td>\n",
       "      <td>5.699</td>\n",
       "      <td>5.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-27</th>\n",
       "      <td>5.637</td>\n",
       "      <td>5.734</td>\n",
       "      <td>5.777</td>\n",
       "      <td>5.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-28</th>\n",
       "      <td>5.765</td>\n",
       "      <td>5.795</td>\n",
       "      <td>5.812</td>\n",
       "      <td>5.702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-08</th>\n",
       "      <td>5.781</td>\n",
       "      <td>5.715</td>\n",
       "      <td>5.809</td>\n",
       "      <td>5.663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-09</th>\n",
       "      <td>5.718</td>\n",
       "      <td>5.827</td>\n",
       "      <td>5.859</td>\n",
       "      <td>5.718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-10</th>\n",
       "      <td>5.827</td>\n",
       "      <td>5.640</td>\n",
       "      <td>5.848</td>\n",
       "      <td>5.629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-11</th>\n",
       "      <td>5.626</td>\n",
       "      <td>5.585</td>\n",
       "      <td>5.702</td>\n",
       "      <td>5.570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-12</th>\n",
       "      <td>5.609</td>\n",
       "      <td>5.624</td>\n",
       "      <td>5.726</td>\n",
       "      <td>5.320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-08</th>\n",
       "      <td>1152.000</td>\n",
       "      <td>1159.760</td>\n",
       "      <td>1166.310</td>\n",
       "      <td>1152.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-09</th>\n",
       "      <td>1166.000</td>\n",
       "      <td>1158.500</td>\n",
       "      <td>1169.000</td>\n",
       "      <td>1147.960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-10</th>\n",
       "      <td>1157.880</td>\n",
       "      <td>1165.000</td>\n",
       "      <td>1181.500</td>\n",
       "      <td>1153.500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-13</th>\n",
       "      <td>1165.000</td>\n",
       "      <td>1162.300</td>\n",
       "      <td>1169.980</td>\n",
       "      <td>1148.500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-14</th>\n",
       "      <td>1176.000</td>\n",
       "      <td>1185.100</td>\n",
       "      <td>1191.550</td>\n",
       "      <td>1166.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-15</th>\n",
       "      <td>1192.000</td>\n",
       "      <td>1189.600</td>\n",
       "      <td>1203.990</td>\n",
       "      <td>1187.100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-16</th>\n",
       "      <td>1192.970</td>\n",
       "      <td>1197.100</td>\n",
       "      <td>1197.100</td>\n",
       "      <td>1184.100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-17</th>\n",
       "      <td>1210.000</td>\n",
       "      <td>1226.000</td>\n",
       "      <td>1234.560</td>\n",
       "      <td>1205.010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-20</th>\n",
       "      <td>1221.000</td>\n",
       "      <td>1227.300</td>\n",
       "      <td>1231.500</td>\n",
       "      <td>1216.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-21</th>\n",
       "      <td>1221.020</td>\n",
       "      <td>1200.000</td>\n",
       "      <td>1223.990</td>\n",
       "      <td>1193.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-22</th>\n",
       "      <td>1206.000</td>\n",
       "      <td>1244.500</td>\n",
       "      <td>1249.500</td>\n",
       "      <td>1202.220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-23</th>\n",
       "      <td>1250.000</td>\n",
       "      <td>1252.260</td>\n",
       "      <td>1265.680</td>\n",
       "      <td>1247.770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-24</th>\n",
       "      <td>1248.000</td>\n",
       "      <td>1250.560</td>\n",
       "      <td>1259.890</td>\n",
       "      <td>1235.180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-27</th>\n",
       "      <td>1257.000</td>\n",
       "      <td>1276.000</td>\n",
       "      <td>1278.170</td>\n",
       "      <td>1250.960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-28</th>\n",
       "      <td>1285.310</td>\n",
       "      <td>1279.130</td>\n",
       "      <td>1299.940</td>\n",
       "      <td>1271.880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-29</th>\n",
       "      <td>1277.800</td>\n",
       "      <td>1274.900</td>\n",
       "      <td>1288.100</td>\n",
       "      <td>1258.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04-30</th>\n",
       "      <td>1271.000</td>\n",
       "      <td>1265.700</td>\n",
       "      <td>1285.010</td>\n",
       "      <td>1258.880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-06</th>\n",
       "      <td>1251.000</td>\n",
       "      <td>1300.000</td>\n",
       "      <td>1300.000</td>\n",
       "      <td>1250.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-07</th>\n",
       "      <td>1299.400</td>\n",
       "      <td>1312.000</td>\n",
       "      <td>1314.990</td>\n",
       "      <td>1293.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-08</th>\n",
       "      <td>1317.000</td>\n",
       "      <td>1314.610</td>\n",
       "      <td>1338.000</td>\n",
       "      <td>1308.510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-11</th>\n",
       "      <td>1320.000</td>\n",
       "      <td>1323.010</td>\n",
       "      <td>1335.000</td>\n",
       "      <td>1313.670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-12</th>\n",
       "      <td>1318.000</td>\n",
       "      <td>1333.000</td>\n",
       "      <td>1334.990</td>\n",
       "      <td>1316.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-13</th>\n",
       "      <td>1333.000</td>\n",
       "      <td>1335.950</td>\n",
       "      <td>1337.990</td>\n",
       "      <td>1322.880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-14</th>\n",
       "      <td>1330.000</td>\n",
       "      <td>1326.590</td>\n",
       "      <td>1334.880</td>\n",
       "      <td>1325.110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-15</th>\n",
       "      <td>1329.000</td>\n",
       "      <td>1313.000</td>\n",
       "      <td>1333.500</td>\n",
       "      <td>1301.880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-18</th>\n",
       "      <td>1306.000</td>\n",
       "      <td>1346.210</td>\n",
       "      <td>1351.500</td>\n",
       "      <td>1305.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-19</th>\n",
       "      <td>1364.000</td>\n",
       "      <td>1346.110</td>\n",
       "      <td>1364.000</td>\n",
       "      <td>1338.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-20</th>\n",
       "      <td>1346.110</td>\n",
       "      <td>1351.000</td>\n",
       "      <td>1359.180</td>\n",
       "      <td>1336.810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-21</th>\n",
       "      <td>1355.000</td>\n",
       "      <td>1366.100</td>\n",
       "      <td>1371.960</td>\n",
       "      <td>1352.860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-22</th>\n",
       "      <td>1366.000</td>\n",
       "      <td>1328.200</td>\n",
       "      <td>1366.100</td>\n",
       "      <td>1322.880</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4470 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                open     close      high       low\n",
       "date                                              \n",
       "2001-08-27     5.392     5.554     5.902     5.132\n",
       "2001-08-28     5.467     5.759     5.781     5.407\n",
       "2001-08-29     5.777     5.684     5.781     5.640\n",
       "2001-08-30     5.668     5.796     5.860     5.624\n",
       "2001-08-31     5.804     5.782     5.877     5.749\n",
       "2001-09-03     5.812     5.779     5.870     5.757\n",
       "2001-09-04     5.782     5.852     5.949     5.762\n",
       "2001-09-05     5.876     5.849     5.924     5.813\n",
       "2001-09-06     5.835     5.734     5.854     5.704\n",
       "2001-09-07     5.702     5.574     5.773     5.570\n",
       "2001-09-10     5.531     5.734     5.757     5.470\n",
       "2001-09-11     5.749     5.670     5.773     5.656\n",
       "2001-09-12     5.520     5.621     5.656     5.515\n",
       "2001-09-13     5.626     5.607     5.671     5.577\n",
       "2001-09-14     5.626     5.671     5.702     5.593\n",
       "2001-09-17     5.637     5.599     5.670     5.546\n",
       "2001-09-18     5.606     5.663     5.710     5.601\n",
       "2001-09-19     5.671     5.768     5.768     5.634\n",
       "2001-09-20     5.765     5.720     5.788     5.702\n",
       "2001-09-21     5.668     5.634     5.718     5.624\n",
       "2001-09-24     5.634     5.632     5.685     5.624\n",
       "2001-09-25     5.668     5.660     5.709     5.632\n",
       "2001-09-26     5.642     5.637     5.699     5.624\n",
       "2001-09-27     5.637     5.734     5.777     5.624\n",
       "2001-09-28     5.765     5.795     5.812     5.702\n",
       "2001-10-08     5.781     5.715     5.809     5.663\n",
       "2001-10-09     5.718     5.827     5.859     5.718\n",
       "2001-10-10     5.827     5.640     5.848     5.629\n",
       "2001-10-11     5.626     5.585     5.702     5.570\n",
       "2001-10-12     5.609     5.624     5.726     5.320\n",
       "...              ...       ...       ...       ...\n",
       "2020-04-08  1152.000  1159.760  1166.310  1152.000\n",
       "2020-04-09  1166.000  1158.500  1169.000  1147.960\n",
       "2020-04-10  1157.880  1165.000  1181.500  1153.500\n",
       "2020-04-13  1165.000  1162.300  1169.980  1148.500\n",
       "2020-04-14  1176.000  1185.100  1191.550  1166.000\n",
       "2020-04-15  1192.000  1189.600  1203.990  1187.100\n",
       "2020-04-16  1192.970  1197.100  1197.100  1184.100\n",
       "2020-04-17  1210.000  1226.000  1234.560  1205.010\n",
       "2020-04-20  1221.000  1227.300  1231.500  1216.800\n",
       "2020-04-21  1221.020  1200.000  1223.990  1193.000\n",
       "2020-04-22  1206.000  1244.500  1249.500  1202.220\n",
       "2020-04-23  1250.000  1252.260  1265.680  1247.770\n",
       "2020-04-24  1248.000  1250.560  1259.890  1235.180\n",
       "2020-04-27  1257.000  1276.000  1278.170  1250.960\n",
       "2020-04-28  1285.310  1279.130  1299.940  1271.880\n",
       "2020-04-29  1277.800  1274.900  1288.100  1258.000\n",
       "2020-04-30  1271.000  1265.700  1285.010  1258.880\n",
       "2020-05-06  1251.000  1300.000  1300.000  1250.000\n",
       "2020-05-07  1299.400  1312.000  1314.990  1293.000\n",
       "2020-05-08  1317.000  1314.610  1338.000  1308.510\n",
       "2020-05-11  1320.000  1323.010  1335.000  1313.670\n",
       "2020-05-12  1318.000  1333.000  1334.990  1316.000\n",
       "2020-05-13  1333.000  1335.950  1337.990  1322.880\n",
       "2020-05-14  1330.000  1326.590  1334.880  1325.110\n",
       "2020-05-15  1329.000  1313.000  1333.500  1301.880\n",
       "2020-05-18  1306.000  1346.210  1351.500  1305.130\n",
       "2020-05-19  1364.000  1346.110  1364.000  1338.000\n",
       "2020-05-20  1346.110  1351.000  1359.180  1336.810\n",
       "2020-05-21  1355.000  1366.100  1371.960  1352.860\n",
       "2020-05-22  1366.000  1328.200  1366.100  1322.880\n",
       "\n",
       "[4470 rows x 4 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输出该股票所有受怕比开票上涨3%的以上的日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2001-08-27', '2001-08-28', '2001-09-10', '2001-12-21',\n",
       "               '2002-01-18', '2002-01-31', '2003-01-14', '2003-10-29',\n",
       "               '2004-01-05', '2004-01-14',\n",
       "               ...\n",
       "               '2019-09-12', '2019-09-18', '2020-02-11', '2020-03-02',\n",
       "               '2020-03-05', '2020-03-10', '2020-04-02', '2020-04-22',\n",
       "               '2020-05-06', '2020-05-18'],\n",
       "              dtype='datetime64[ns]', name='date', length=311, freq=None)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[(df['close']-df['open'])/df['open']>=0.03].index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输出该股票所有开票比前日受怕跌幅超过2%的日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2001-09-12', '2002-06-26', '2002-12-13', '2004-07-01',\n",
       "               '2004-10-29', '2006-08-21', '2006-08-23', '2007-01-25',\n",
       "               '2007-02-01', '2007-02-06', '2007-03-19', '2007-05-21',\n",
       "               '2007-05-30', '2007-06-05', '2007-07-27', '2007-09-05',\n",
       "               '2007-09-10', '2008-03-13', '2008-03-17', '2008-03-25',\n",
       "               '2008-03-27', '2008-04-22', '2008-04-23', '2008-04-29',\n",
       "               '2008-05-13', '2008-06-10', '2008-06-13', '2008-06-24',\n",
       "               '2008-06-27', '2008-08-11', '2008-08-19', '2008-09-23',\n",
       "               '2008-10-10', '2008-10-15', '2008-10-16', '2008-10-20',\n",
       "               '2008-10-23', '2008-10-27', '2008-11-06', '2008-11-12',\n",
       "               '2008-11-20', '2008-11-21', '2008-12-02', '2009-02-27',\n",
       "               '2009-03-25', '2009-08-13', '2010-04-26', '2010-04-30',\n",
       "               '2011-08-05', '2012-03-27', '2012-08-10', '2012-11-22',\n",
       "               '2012-12-04', '2012-12-24', '2013-01-16', '2013-01-25',\n",
       "               '2013-09-02', '2014-04-25', '2015-01-19', '2015-05-25',\n",
       "               '2015-07-03', '2015-07-08', '2015-07-13', '2015-08-24',\n",
       "               '2015-09-02', '2015-09-15', '2017-11-17', '2018-02-06',\n",
       "               '2018-02-09', '2018-03-23', '2018-03-28', '2018-07-11',\n",
       "               '2018-10-11', '2018-10-24', '2018-10-25', '2018-10-29',\n",
       "               '2018-10-30', '2019-05-06', '2019-05-08', '2019-10-16',\n",
       "               '2020-01-02', '2020-02-03', '2020-03-13', '2020-03-23'],\n",
       "              dtype='datetime64[ns]', name='date', freq=None)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# shift移动位置，正1是往后移一位，负1是往前移一位，这样就可以前后比\n",
    "df[(df['open']-df['close'].shift(1))/df['close'].shift(1)<=-0.02].index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 假如我从2021年1月1日开始，每月第一个交易日以开盘价（收盘价也可以）买入1手股票，每年最后一个交易日卖出所有股票，到今天为止，我的收益如何？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2001-09-03</th>\n",
       "      <td>5.812</td>\n",
       "      <td>5.779</td>\n",
       "      <td>5.870</td>\n",
       "      <td>5.757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-04</th>\n",
       "      <td>5.782</td>\n",
       "      <td>5.852</td>\n",
       "      <td>5.949</td>\n",
       "      <td>5.762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-05</th>\n",
       "      <td>5.876</td>\n",
       "      <td>5.849</td>\n",
       "      <td>5.924</td>\n",
       "      <td>5.813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-06</th>\n",
       "      <td>5.835</td>\n",
       "      <td>5.734</td>\n",
       "      <td>5.854</td>\n",
       "      <td>5.704</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-07</th>\n",
       "      <td>5.702</td>\n",
       "      <td>5.574</td>\n",
       "      <td>5.773</td>\n",
       "      <td>5.570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-10</th>\n",
       "      <td>5.531</td>\n",
       "      <td>5.734</td>\n",
       "      <td>5.757</td>\n",
       "      <td>5.470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-11</th>\n",
       "      <td>5.749</td>\n",
       "      <td>5.670</td>\n",
       "      <td>5.773</td>\n",
       "      <td>5.656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-12</th>\n",
       "      <td>5.520</td>\n",
       "      <td>5.621</td>\n",
       "      <td>5.656</td>\n",
       "      <td>5.515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-13</th>\n",
       "      <td>5.626</td>\n",
       "      <td>5.607</td>\n",
       "      <td>5.671</td>\n",
       "      <td>5.577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-14</th>\n",
       "      <td>5.626</td>\n",
       "      <td>5.671</td>\n",
       "      <td>5.702</td>\n",
       "      <td>5.593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-17</th>\n",
       "      <td>5.637</td>\n",
       "      <td>5.599</td>\n",
       "      <td>5.670</td>\n",
       "      <td>5.546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-18</th>\n",
       "      <td>5.606</td>\n",
       "      <td>5.663</td>\n",
       "      <td>5.710</td>\n",
       "      <td>5.601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-19</th>\n",
       "      <td>5.671</td>\n",
       "      <td>5.768</td>\n",
       "      <td>5.768</td>\n",
       "      <td>5.634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-20</th>\n",
       "      <td>5.765</td>\n",
       "      <td>5.720</td>\n",
       "      <td>5.788</td>\n",
       "      <td>5.702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-21</th>\n",
       "      <td>5.668</td>\n",
       "      <td>5.634</td>\n",
       "      <td>5.718</td>\n",
       "      <td>5.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-24</th>\n",
       "      <td>5.634</td>\n",
       "      <td>5.632</td>\n",
       "      <td>5.685</td>\n",
       "      <td>5.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-25</th>\n",
       "      <td>5.668</td>\n",
       "      <td>5.660</td>\n",
       "      <td>5.709</td>\n",
       "      <td>5.632</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-26</th>\n",
       "      <td>5.642</td>\n",
       "      <td>5.637</td>\n",
       "      <td>5.699</td>\n",
       "      <td>5.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-27</th>\n",
       "      <td>5.637</td>\n",
       "      <td>5.734</td>\n",
       "      <td>5.777</td>\n",
       "      <td>5.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-09-28</th>\n",
       "      <td>5.765</td>\n",
       "      <td>5.795</td>\n",
       "      <td>5.812</td>\n",
       "      <td>5.702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-08</th>\n",
       "      <td>5.781</td>\n",
       "      <td>5.715</td>\n",
       "      <td>5.809</td>\n",
       "      <td>5.663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-09</th>\n",
       "      <td>5.718</td>\n",
       "      <td>5.827</td>\n",
       "      <td>5.859</td>\n",
       "      <td>5.718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-10</th>\n",
       "      <td>5.827</td>\n",
       "      <td>5.640</td>\n",
       "      <td>5.848</td>\n",
       "      <td>5.629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-11</th>\n",
       "      <td>5.626</td>\n",
       "      <td>5.585</td>\n",
       "      <td>5.702</td>\n",
       "      <td>5.570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-12</th>\n",
       "      <td>5.609</td>\n",
       "      <td>5.624</td>\n",
       "      <td>5.726</td>\n",
       "      <td>5.320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-15</th>\n",
       "      <td>5.609</td>\n",
       "      <td>5.515</td>\n",
       "      <td>5.624</td>\n",
       "      <td>5.452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-16</th>\n",
       "      <td>5.488</td>\n",
       "      <td>5.437</td>\n",
       "      <td>5.593</td>\n",
       "      <td>5.429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-17</th>\n",
       "      <td>5.435</td>\n",
       "      <td>5.343</td>\n",
       "      <td>5.438</td>\n",
       "      <td>5.296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-18</th>\n",
       "      <td>5.343</td>\n",
       "      <td>5.318</td>\n",
       "      <td>5.415</td>\n",
       "      <td>5.298</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-10-19</th>\n",
       "      <td>5.312</td>\n",
       "      <td>5.234</td>\n",
       "      <td>5.335</td>\n",
       "      <td>5.157</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-10</th>\n",
       "      <td>1109.000</td>\n",
       "      <td>1112.500</td>\n",
       "      <td>1115.990</td>\n",
       "      <td>1102.500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-13</th>\n",
       "      <td>1112.500</td>\n",
       "      <td>1124.270</td>\n",
       "      <td>1129.200</td>\n",
       "      <td>1112.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-14</th>\n",
       "      <td>1124.200</td>\n",
       "      <td>1107.400</td>\n",
       "      <td>1124.890</td>\n",
       "      <td>1103.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-15</th>\n",
       "      <td>1109.010</td>\n",
       "      <td>1112.130</td>\n",
       "      <td>1121.600</td>\n",
       "      <td>1105.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-16</th>\n",
       "      <td>1118.870</td>\n",
       "      <td>1107.000</td>\n",
       "      <td>1118.870</td>\n",
       "      <td>1102.580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-17</th>\n",
       "      <td>1110.000</td>\n",
       "      <td>1107.500</td>\n",
       "      <td>1112.780</td>\n",
       "      <td>1101.010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-20</th>\n",
       "      <td>1111.860</td>\n",
       "      <td>1091.000</td>\n",
       "      <td>1111.860</td>\n",
       "      <td>1082.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-21</th>\n",
       "      <td>1081.000</td>\n",
       "      <td>1075.300</td>\n",
       "      <td>1087.000</td>\n",
       "      <td>1072.300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-22</th>\n",
       "      <td>1070.000</td>\n",
       "      <td>1075.510</td>\n",
       "      <td>1084.000</td>\n",
       "      <td>1055.380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-23</th>\n",
       "      <td>1076.000</td>\n",
       "      <td>1052.800</td>\n",
       "      <td>1076.000</td>\n",
       "      <td>1037.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-03</th>\n",
       "      <td>985.000</td>\n",
       "      <td>1003.920</td>\n",
       "      <td>1010.680</td>\n",
       "      <td>980.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-04</th>\n",
       "      <td>1015.000</td>\n",
       "      <td>1038.010</td>\n",
       "      <td>1057.000</td>\n",
       "      <td>1011.010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-05</th>\n",
       "      <td>1050.000</td>\n",
       "      <td>1049.990</td>\n",
       "      <td>1054.000</td>\n",
       "      <td>1033.030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-06</th>\n",
       "      <td>1059.430</td>\n",
       "      <td>1071.000</td>\n",
       "      <td>1075.000</td>\n",
       "      <td>1052.020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-07</th>\n",
       "      <td>1070.010</td>\n",
       "      <td>1076.000</td>\n",
       "      <td>1077.000</td>\n",
       "      <td>1061.020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-10</th>\n",
       "      <td>1062.000</td>\n",
       "      <td>1066.490</td>\n",
       "      <td>1074.600</td>\n",
       "      <td>1057.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-11</th>\n",
       "      <td>1063.000</td>\n",
       "      <td>1098.000</td>\n",
       "      <td>1099.680</td>\n",
       "      <td>1062.800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-12</th>\n",
       "      <td>1089.000</td>\n",
       "      <td>1097.270</td>\n",
       "      <td>1098.790</td>\n",
       "      <td>1085.880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-13</th>\n",
       "      <td>1098.000</td>\n",
       "      <td>1091.000</td>\n",
       "      <td>1113.890</td>\n",
       "      <td>1088.010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-14</th>\n",
       "      <td>1090.450</td>\n",
       "      <td>1088.000</td>\n",
       "      <td>1093.510</td>\n",
       "      <td>1083.110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-17</th>\n",
       "      <td>1082.500</td>\n",
       "      <td>1093.820</td>\n",
       "      <td>1096.190</td>\n",
       "      <td>1082.400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-18</th>\n",
       "      <td>1090.010</td>\n",
       "      <td>1084.000</td>\n",
       "      <td>1096.880</td>\n",
       "      <td>1083.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-19</th>\n",
       "      <td>1085.000</td>\n",
       "      <td>1096.500</td>\n",
       "      <td>1101.500</td>\n",
       "      <td>1078.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-20</th>\n",
       "      <td>1096.700</td>\n",
       "      <td>1118.000</td>\n",
       "      <td>1124.000</td>\n",
       "      <td>1095.410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-21</th>\n",
       "      <td>1111.460</td>\n",
       "      <td>1112.880</td>\n",
       "      <td>1123.540</td>\n",
       "      <td>1110.010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-24</th>\n",
       "      <td>1105.500</td>\n",
       "      <td>1087.180</td>\n",
       "      <td>1110.000</td>\n",
       "      <td>1085.500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-25</th>\n",
       "      <td>1078.000</td>\n",
       "      <td>1074.810</td>\n",
       "      <td>1082.090</td>\n",
       "      <td>1067.880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-26</th>\n",
       "      <td>1062.000</td>\n",
       "      <td>1073.700</td>\n",
       "      <td>1084.000</td>\n",
       "      <td>1055.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-27</th>\n",
       "      <td>1076.000</td>\n",
       "      <td>1087.390</td>\n",
       "      <td>1094.990</td>\n",
       "      <td>1076.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-28</th>\n",
       "      <td>1070.300</td>\n",
       "      <td>1057.000</td>\n",
       "      <td>1082.000</td>\n",
       "      <td>1049.970</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4409 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                open     close      high       low\n",
       "date                                              \n",
       "2001-09-03     5.812     5.779     5.870     5.757\n",
       "2001-09-04     5.782     5.852     5.949     5.762\n",
       "2001-09-05     5.876     5.849     5.924     5.813\n",
       "2001-09-06     5.835     5.734     5.854     5.704\n",
       "2001-09-07     5.702     5.574     5.773     5.570\n",
       "2001-09-10     5.531     5.734     5.757     5.470\n",
       "2001-09-11     5.749     5.670     5.773     5.656\n",
       "2001-09-12     5.520     5.621     5.656     5.515\n",
       "2001-09-13     5.626     5.607     5.671     5.577\n",
       "2001-09-14     5.626     5.671     5.702     5.593\n",
       "2001-09-17     5.637     5.599     5.670     5.546\n",
       "2001-09-18     5.606     5.663     5.710     5.601\n",
       "2001-09-19     5.671     5.768     5.768     5.634\n",
       "2001-09-20     5.765     5.720     5.788     5.702\n",
       "2001-09-21     5.668     5.634     5.718     5.624\n",
       "2001-09-24     5.634     5.632     5.685     5.624\n",
       "2001-09-25     5.668     5.660     5.709     5.632\n",
       "2001-09-26     5.642     5.637     5.699     5.624\n",
       "2001-09-27     5.637     5.734     5.777     5.624\n",
       "2001-09-28     5.765     5.795     5.812     5.702\n",
       "2001-10-08     5.781     5.715     5.809     5.663\n",
       "2001-10-09     5.718     5.827     5.859     5.718\n",
       "2001-10-10     5.827     5.640     5.848     5.629\n",
       "2001-10-11     5.626     5.585     5.702     5.570\n",
       "2001-10-12     5.609     5.624     5.726     5.320\n",
       "2001-10-15     5.609     5.515     5.624     5.452\n",
       "2001-10-16     5.488     5.437     5.593     5.429\n",
       "2001-10-17     5.435     5.343     5.438     5.296\n",
       "2001-10-18     5.343     5.318     5.415     5.298\n",
       "2001-10-19     5.312     5.234     5.335     5.157\n",
       "...              ...       ...       ...       ...\n",
       "2020-01-10  1109.000  1112.500  1115.990  1102.500\n",
       "2020-01-13  1112.500  1124.270  1129.200  1112.000\n",
       "2020-01-14  1124.200  1107.400  1124.890  1103.000\n",
       "2020-01-15  1109.010  1112.130  1121.600  1105.000\n",
       "2020-01-16  1118.870  1107.000  1118.870  1102.580\n",
       "2020-01-17  1110.000  1107.500  1112.780  1101.010\n",
       "2020-01-20  1111.860  1091.000  1111.860  1082.000\n",
       "2020-01-21  1081.000  1075.300  1087.000  1072.300\n",
       "2020-01-22  1070.000  1075.510  1084.000  1055.380\n",
       "2020-01-23  1076.000  1052.800  1076.000  1037.000\n",
       "2020-02-03   985.000  1003.920  1010.680   980.000\n",
       "2020-02-04  1015.000  1038.010  1057.000  1011.010\n",
       "2020-02-05  1050.000  1049.990  1054.000  1033.030\n",
       "2020-02-06  1059.430  1071.000  1075.000  1052.020\n",
       "2020-02-07  1070.010  1076.000  1077.000  1061.020\n",
       "2020-02-10  1062.000  1066.490  1074.600  1057.200\n",
       "2020-02-11  1063.000  1098.000  1099.680  1062.800\n",
       "2020-02-12  1089.000  1097.270  1098.790  1085.880\n",
       "2020-02-13  1098.000  1091.000  1113.890  1088.010\n",
       "2020-02-14  1090.450  1088.000  1093.510  1083.110\n",
       "2020-02-17  1082.500  1093.820  1096.190  1082.400\n",
       "2020-02-18  1090.010  1084.000  1096.880  1083.000\n",
       "2020-02-19  1085.000  1096.500  1101.500  1078.000\n",
       "2020-02-20  1096.700  1118.000  1124.000  1095.410\n",
       "2020-02-21  1111.460  1112.880  1123.540  1110.010\n",
       "2020-02-24  1105.500  1087.180  1110.000  1085.500\n",
       "2020-02-25  1078.000  1074.810  1082.090  1067.880\n",
       "2020-02-26  1062.000  1073.700  1084.000  1055.000\n",
       "2020-02-27  1076.000  1087.390  1094.990  1076.000\n",
       "2020-02-28  1070.300  1057.000  1082.000  1049.970\n",
       "\n",
       "[4409 rows x 4 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "price_last = df['open'][-1]  # 最后一天的开盘价\n",
    "df = df['2001-09':'2020-02']  # 删除首尾无用的数据，即没有月初或月末的\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_monthly = df.resample(\"MS\").first()  # 取第一个月,去掉S标识是末天的标识，但数据还是月初的\n",
    "df_yearly = df.resample(\"A\").last()[:-1]  # 取一年的最后交易日的值，并去掉没有12月的20年（现在时间是20年05月）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最终赚了668979元\n"
     ]
    }
   ],
   "source": [
    "cost_money = 0  # 花多少钱\n",
    "hold = 0  # 现在有多少股票\n",
    "for year in range(2001,2021):\n",
    "    cost_money += df_monthly[str(year)]['open'].sum()*100  # 每年第一个月买100股花的钱\n",
    "    hold += len(df_monthly[str(year)]['open'])*100  # 持有的股票\n",
    "    if year != 2020:  # 如果不是最后一年，则需要把股票卖出\n",
    "        cost_money -= df_yearly[str(year)]['open'][0]*hold  # 获取开盘价并卖出去\n",
    "        hold = 0\n",
    "cost_money -= hold * price_last\n",
    "print('最终赚了%d元'%(-cost_money))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
